package me.zhengjie.cron;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modbusTCP.EntranceTCP;
import me.zhengjie.modbusTCP.PalletTCP;
import me.zhengjie.pojo.sqlServer.PPOutboundMission;
import me.zhengjie.pojo.sqlServer.PPStation;
import me.zhengjie.pojo.sqlServer.PPTaskResult;
import me.zhengjie.pojo.sqlServer.PpPallet;
import me.zhengjie.ppService.PPOutBoundService;
import me.zhengjie.ppService.PPStationService;
import me.zhengjie.ppService.PPTaskResultService;
import me.zhengjie.repository.PPOutboundMissionRepository;
import me.zhengjie.repository.PPStationRepository;
import me.zhengjie.wcs.PPWcsStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: xinglin
 * @CreateTime: 2024-08-16
 * @Description: pp出库任务定时执行
 * @Version: 1.0
 */


@Slf4j
@Component
@RequiredArgsConstructor
public class PPOutBoundCron {

    private final PPWcsStatus ppWcsStatus;
    private final PalletTCP palletTCP;
    private final EntranceTCP entranceTCP;
    private final PPTaskResultService ppTaskResultService;
    private final PPOutBoundService ppOutBoundService;
    private final PPStationService ppStationService;
    private final PPStationRepository ppStationRepository;
    private final PPOutboundMissionRepository ppOutboundMissionRepository;
    //公平锁
    //private final Lock lock = new ReentrantLock();

    private static final Logger ppLog = LoggerFactory.getLogger("pp");


    @Scheduled(fixedDelay = 3000)
   //@Transactional
    public void StartOutBoundTask() {


        if (!ppWcsStatus.allowSendTask()) {
            ppLog.error("当前码垛机不能接收新任务");
        } else {

                    // 查找有没有正在进行的出库任务
                    PPTaskResult ppTaskResult = ppTaskResultService.findNowppTaskResult(2);
                    // 查找最新的出库任务
                    PPOutboundMission newPPOutboundMission = ppOutBoundService.findNewOutBoundMission();

                    //查询有无空闲的站点
                    List<PPStation> freeStations = ppStationService.findFreeStation();

                    if (ObjectUtil.isEmpty(ppTaskResult) && ObjectUtil.isNotEmpty(newPPOutboundMission)) {
                        ppLog.info("查询到码垛机空闲，开始发送出库任务");
                        //查看是否有分配终点
                        if (newPPOutboundMission.getDestination() == null && freeStations.size() > 0) {

                            //任务分配终点
                            newPPOutboundMission.setDestination(freeStations.get(0).getStationCode());
                            //锁对应的工作站
                            ppStationService.lockStation(newPPOutboundMission.getDestination());

                            ppOutboundMissionRepository.save(newPPOutboundMission);

                            //创建pp_wms_task
                            ppOutBoundService.createOutTask(newPPOutboundMission);

                            //发送出库任务到wcs
                            ppOutBoundService.sendOutBoundTask(newPPOutboundMission);
                            ppLog.info("发送出库任务到wcs成功，任务wms_task_id：{}", newPPOutboundMission.getMissionCode());

                        }

                        if (newPPOutboundMission.getDestination() != null) {
                            //查询对应工作站信息
                            PPStation ppStation = ppStationRepository.findPPStationsByStationCode(newPPOutboundMission.getDestination());

                            //判断工作站是否空闲
                            if (ppStation.getTakeStatus() == 0) {
                                //创建pp_wms_task
                                ppOutBoundService.createOutTask(newPPOutboundMission);

                                //锁对应的工作站
                                ppStationService.lockStation(newPPOutboundMission.getDestination());

                                //发送出库任务到wcs
                                ppOutBoundService.sendOutBoundTask(newPPOutboundMission);
                                ppLog.info("发送出库任务到wcs成功，任务wms_task_id：{}", newPPOutboundMission.getMissionCode());
                            }
                        }
                    }
        }


    }


}






